package com.fywsy.fyweb.strategy;

import com.fywsy.fyweb.entity.SendItem;
import com.fywsy.fyweb.utils.RestTemplateUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * 游戏后台实现类
 * 注解gameId对应数据库游戏id
 * 注解GongnengMethod对应数据库功能id
 */
@Component
@GameId(14)
public class WdJinsheGameStrategy extends BaseGameStrategy {
    public static final String PAY = "http://111.170.174.216:81/shanshangm/zxcz/pay.php";

    public static final String MAIL = "http://111.170.174.216:81/shanshangm/test1.php";

    public static final String LOGIN = "http://111.170.174.216:82/admin/ajax.php?act=login";

    public static final String REAL_PAY = "http://111.170.174.216:82/admin/ajax1.php?act=buy_shop";

    private static String cookie;
    private static long cookieExpireTime; // Cookie过期时间戳
    private static final long COOKIE_EXPIRE_HOURS = 1; // Cookie有效期（小时）

    @GongnengMethod(1401)
    public String pay(SendItem item) {
        MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
        params.add("gmcode", "diyao88");
        params.add("userName", item.uid);
        params.add("type", "gold_coin_" + item.itemId);
        return RestTemplateUtil.sendPostFormAndGetJsonKey(PAY, params, "message");
    }

    @GongnengMethod(1402)
    public String mail(SendItem item) {
        MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
        if("1".equals(item.itemId)) { //潜能
            params.add("c3", item.uid);
            params.add("c4", "潜能");
            params.add("c6", item.number);
        }
        return RestTemplateUtil.sendPostFormAndGetJsonKey(MAIL, params, "msg");
    }

    @GongnengMethod(1403)
    public String realPay(SendItem item) {
        String cookie = getCookie();
        if (cookie.isEmpty()) {
            return "登录失败";
        }
        
        // 发送礼包请求
        MultiValueMap<String, String> giftParams = new LinkedMultiValueMap<>();
        giftParams.add("serverid", "1");
        giftParams.add("rolename", item.uid);
        giftParams.add("giftid", item.itemId);
        
        return RestTemplateUtil.sendPostFormAndGetJsonKeyWithCookie(REAL_PAY, giftParams, "msg", cookie);
    }


    private boolean isCookieExpired() {
        if (cookie == null) {
            return true;
        }
        long currentTime = System.currentTimeMillis();
        return currentTime >= cookieExpireTime;
    }

    private String getCookie() {
        if (isCookieExpired()) {
            // 登录获取cookie
            MultiValueMap<String, String> loginParams = new LinkedMultiValueMap<>();
            loginParams.add("username", "admin");
            loginParams.add("password", "diyao88");
            loginParams.add("code", "shan");
            loginParams.add("remember", "on");

            cookie = RestTemplateUtil.sendPostFormAndGetCookie(LOGIN, loginParams);
            if (!cookie.isEmpty()) {
                // 设置cookie过期时间为1小时后
                cookieExpireTime = System.currentTimeMillis() + (COOKIE_EXPIRE_HOURS * 60 * 60 * 1000);
            }
        }
        return cookie;
    }

    @GongnengMethod(4)
    public String cleat(SendItem item) {
        System.out.println("清除成功");
        return "清除成功";
    }

    public static void main(String[] args) {
        SendItem item = new SendItem();
        item.uid = "风云";
        item.itemId = "23";
        System.out.println(new WdJinsheGameStrategy().realPay(item));
    }
}

